package com.kaba.xtzhgl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kaba.bean.*;
import com.kaba.xtzhgl.service.SysRoleMenuService;
import com.kaba.xtzhgl.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Author lvmengjie
 * @Date 2021/3/12
 * @Description TODO
 */
@RequestMapping("/sysrole")
@RestController
@CrossOrigin
public class SysRoleContorller {
    @Autowired
    private  SysRoleService sysRoleService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    /**
     * 列表显示
     * */

    @GetMapping("/queryrole")
    public ResponseResult queryrole(){
        List<SysRole> list =sysRoleService.queryRole();
        return ResponseResult.success(ResultCode.SUCCESS,list);

    }

    @GetMapping("/queryrolemenu")
    public ResponseResult queryrolemenu(){
        List<SysRoleMenu> list =sysRoleMenuService.list();
        return ResponseResult.success(ResultCode.SUCCESS,list);

    }


    /**
     * 综合查询
     * */
    @PostMapping("zhcx")
    public ResponseResult zhcx(@RequestBody SysRole sysRole){
        List<SysRole> lsitrole=sysRoleService.zhcx(sysRole);
        return ResponseResult.success(ResultCode.SUCCESS,lsitrole);

    }

    /**
     * 删除方法
     * */

    @DeleteMapping("deleteRole/{roleId}")
    public ResponseResult deleteRole(@PathVariable Integer roleId){
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("role_id",roleId);
        wrapper.set("del_flag","2");
        wrapper.set("status","1");
        sysRoleService.update(wrapper);
        return  ResponseResult.success(ResultCode.SUCCESS);
    }

    @PostMapping("/saveRole")
    public ResponseResult saveRole(@RequestBody SysRole sysRole){
        sysRoleService.saveRole(sysRole);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("role_key",sysRole.getRoleKey());
        List<SysRole> listrole =sysRoleService.list(wrapper);
        return  ResponseResult.success(ResultCode.SUCCESS,listrole);

    }

    @PostMapping("/saveRoleMenu")
    @ResponseBody
    public ResponseResult saveRoleMenu(  Integer roleId,String menuId){
        System.out.println("----------");
        String[] aa=menuId.split(",");
        for (int i = 0; i < aa.length; i++) {
            System.out.println("角色："+roleId);
            System.out.println("权限id："+Integer.parseInt(aa[i]));
           sysRoleService.saveRoleMenu(roleId,Integer.parseInt(aa[i]));
        }
       return ResponseResult.success(ResultCode.SUCCESS);
    }

    @PutMapping("/updateRole")
    public ResponseResult updateRole(@RequestBody  SysRole sysRole){
        sysRoleService.updateRole(sysRole);
        //修改权限的同时先删除原有权限，然后在赋权
        sysRoleMenuService.deleteRoleMenu(sysRole.getRoleId());
        return  ResponseResult.success(ResultCode.SUCCESS);
    };



}
